<div th:replace="admin/module/_header"></div>

<div class="wrapper">
    <!-- 菜单栏模块 -->
    <div th:replace="admin/module/_sidebar"></div>
    <div class="content-wrapper">
        <section class="content-header">
            <h1 style="display: inline-block;">权限管理</h1>
            <ol class="breadcrumb">
                <li>
                    <a data-pjax="true" href="/admin"><i
                            class="fa fa-dashboard"></i> 首页</a>
                </li>
                <li><a data-pjax="true" href="#">权限管理</a></li>
                <li class="active">修改权限</li>
            </ol>
        </section>
        <section class="content container-fluid">
            <div class="row">
                <div class="col-md-8    ">
                    <div class="nav-tabs-custom">
                        <div class="box box-primary">
                            <div class="box-body table-responsive">
                                <div class="box-header with-border">
                                    <h3 class="box-title">修改权限</h3>
                                </div>
                                <form action="/admin/permission/save" method="post" role="form"
                                      id="permissionUpdateForm">
                                    <div class="box-body">
                                        <input type="hidden" name="id" id="permission-id"
                                               th:data-id="${updatePermission.id}" th:value="${updatePermission.id}">
                                        <div class="form-group">
                                            <label for="name">名称</label>
                                            <input type="text" class="form-control" id="name" name="name"
                                                   th:value="${updatePermission.name}">
                                        </div>
                                        <div class="form-group">
                                            <label for="url">URL</label>
                                            <input type="text" class="form-control" id="url" name="url"
                                                   th:value="${updatePermission.url}">
                                            <small>URL路径</small>
                                        </div>
                                        <div class="form-group">
                                            <label for="sort">排序</label>
                                            <input type="number" class="form-control" id="sort" name="sort"
                                                   th:value="${updatePermission.sort}">
                                            <small>排序字段，越小越靠前，支持小数</small>
                                        </div>
                                        <div class="form-group">
                                            <label for="pid">父节点</label>
                                            <select class="form-control" id="pid" name="pid">
                                                <option value="0">无</option>
                                                <option th:each="permission : ${permissionList}"
                                                        th:if="${permission.resourceType != 'button'}"
                                                        th:class="'permission-level-'+${permission.level}"
                                                        th:value="${permission.id}" th:utext="${permission.name}"
                                                        th:selected="${updatePermission.pid == permission.id }"></option>
                                            </select>
                                        </div>
                                        <div class="form-group">
                                            <label for="icon">图标</label>
                                            <input type="text" class="form-control" id="icon" name="icon"
                                                   th:value="${updatePermission?.icon}">
                                            <small>用于后台菜单图标</small>
                                        </div>
                                        <div class="form-group">
                                            <label for="resourceType">资源类型</label>
                                            <select class="form-control" id="resourceType" name="resourceType"
                                                    th:value="${updatePermission?.resourceType}">
                                                <option value="button"
                                                        th:selected="${updatePermission.resourceType == 'button'}">接口
                                                </option>
                                                <option value="page"
                                                        th:selected="${updatePermission.resourceType == 'page'}">页面
                                                </option>
                                                <option value="menu"
                                                        th:selected="${updatePermission.resourceType == 'menu'}">菜单
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="box-footer">
                                        <button type="submit" class="btn btn-primary btn-sm ">保存</button>
                                        <button type="button" class="btn btn-danger btn-sm "
                                                th:onclick="'removeIt('+${updatePermission.id}+')'">删除
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="nav-tabs-custom">
                        <div class="box box-primary">
                            <div class="box-body table-responsive">
                                <div class="box-header with-border">
                                    <h3 class="box-title">权限树</h3>
                                </div>
                                <div class="form-group">
                                    <ul id="permission" class="ztree"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <script th:inline="javascript">
            // ztree文档地址：http://www.treejs.cn/v3/api.php
            var setting = {
                view: {
                    addHoverDom: false,
                    removeHoverDom: false,
                    selectedMulti: false,
                    showIcon: false,
                    showLine: false
                },
                check: {
                    enable: true,
                    chkboxType: {"Y": "p", "N": ""}
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pid",
                        rootPId: 0
                    }
                },
                edit: {
                    enable: false
                }
            };

            // 初始化
            var permissions = [[${permissions}]]
            $.fn.zTree.init($("#permission"), setting, permissions);

            // 默认勾选
            var treeObj = $.fn.zTree.getZTreeObj("permission");
            var node = treeObj.getNodeByParam("id", $("#permission-id").attr("data-id"));
            treeObj.selectNode(node);
            treeObj.checkNode(node, true, true);


            function removeIt(id) {
                layer.confirm('你确定是否删除', function (index) {
                    $.ajax({
                        type: 'DELETE',
                        url: '/admin/permission/delete?id=' + id,
                        async: false,
                        success: function (data) {
                            if (data.code == 0) {
                                showMsg(data.msg, "error", 1000);
                            } else {
                                window.location.href = "/admin/permission";
                            }

                        }
                    });
                    layer.close(index);
                });
            }

        </script>
    </div>
</div>
<div th:replace="admin/module/_footer"></div>

